{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:20.739405Z",
     "start_time": "2021-11-28T01:43:20.733405Z"
    }
   },
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 定义一个类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:20.814405Z",
     "start_time": "2021-11-28T01:43:20.805405Z"
    }
   },
   "outputs": [],
   "source": [
    "class Warrior(object):\n",
    "    '''\n",
    "    战士职业的类\n",
    "    '''\n",
    "    def __init__(self, name): # 实例化的传入的名字\n",
    "        self.name = name\n",
    "        self.blood = 100\n",
    "        self.damage = 31\n",
    "        self.defance = 30\n",
    "        self.x = 0\n",
    "        self.y = 0\n",
    "    \n",
    "    def move(self):\n",
    "        # 移动\n",
    "        self.x += 1 \n",
    "        self.y += 1 \n",
    "        print('{name}移动到坐标:({x},{y})'.format(\n",
    "            name = self.name,\n",
    "            x = self.x,\n",
    "            y = self.y,\n",
    "            )\n",
    "         ) #这里的self.name就是获取我们传入的name\n",
    "        \n",
    "    def attack(self,targert):\n",
    "        # 发起攻击\n",
    "        targert.blood -= self.damage -  targert.defance\n",
    "#         targert.blood = targert.blood -  (self.damage -  targert.defance)\n",
    "        print('{name} 攻击 {targert} ，造成 {hurt} 点伤害。'.format(\n",
    "            name = self.name,\n",
    "            targert = targert.name,\n",
    "            hurt = self.damage -  targert.defance,\n",
    "            )\n",
    "         ) #这里的self.name就是获取我们传入的name\n",
    "       \n",
    "#     def hurt(self,damage):\n",
    "#         # 受到伤害\n",
    "#         self.blood -= damage -  self.defance\n",
    "# #         self.blood = self.blood - (damage -  self.defance)\n",
    "#         #这里可以放其他制作细节\n",
    "#         print('{name} 受到 {hurt} 点伤害。当前生命值: {blood} '.format(\n",
    "#             name = self.name, \n",
    "#             hurt = damage - self.defance, \n",
    "#             blood = self.blood,\n",
    "#             )\n",
    "#          )\n",
    "    \n",
    "    def medicine(self):\n",
    "        # 恢复技能\n",
    "        blood = self.blood\n",
    "        if self.blood  < 60:\n",
    "            self.blood += 40\n",
    "            recover_point = 40\n",
    "        else:\n",
    "            self.blood = 100\n",
    "            recover_point = 100 - blood\n",
    "        print('{name} 恢复了 {point} 点生命。当前生命值: {blood}'.format(\n",
    "            name = self.name, \n",
    "            point = recover_point, \n",
    "            blood = self.blood,\n",
    "            )\n",
    "         )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 类的复用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:20.883405Z",
     "start_time": "2021-11-28T01:43:20.875405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "名字：Warrior A\n",
      "血量：100\n",
      "攻击力：31\n",
      "防御力：30\n"
     ]
    }
   ],
   "source": [
    "a = Warrior('Warrior A')\n",
    "print('名字：{}'.format(a.name))\n",
    "print('血量：{}'.format(a.blood))\n",
    "print('攻击力：{}'.format(a.damage))\n",
    "print('防御力：{}'.format(a.defance))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:20.920405Z",
     "start_time": "2021-11-28T01:43:20.887405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Warrior A 恢复了 30 点生命。当前生命值: 100\n"
     ]
    }
   ],
   "source": [
    "a.blood = 70\n",
    "a.medicine()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:20.933405Z",
     "start_time": "2021-11-28T01:43:20.922405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "血量：80\n"
     ]
    }
   ],
   "source": [
    "a.blood = 80\n",
    "print('血量：{}'.format(a.blood))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:20.946405Z",
     "start_time": "2021-11-28T01:43:20.936405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Warrior A移动到坐标:(1,1)\n",
      "Warrior A移动到坐标:(2,2)\n",
      "Warrior A移动到坐标:(3,3)\n"
     ]
    }
   ],
   "source": [
    "a.move()\n",
    "a.move()\n",
    "a.move()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:20.963405Z",
     "start_time": "2021-11-28T01:43:20.950405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "名字：Wooden Man\n",
      "血量：100\n",
      "攻击力：31\n",
      "防御力：30\n"
     ]
    }
   ],
   "source": [
    "b = Warrior('Wooden Man')\n",
    "print('名字：{}'.format(b.name))\n",
    "print('血量：{}'.format(b.blood))\n",
    "print('攻击力：{}'.format(b.damage))\n",
    "print('防御力：{}'.format(b.defance))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:20.977405Z",
     "start_time": "2021-11-28T01:43:20.966405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Warrior A 攻击 Wooden Man ，造成 1 点伤害。\n",
      "99\n"
     ]
    }
   ],
   "source": [
    "b.blood = 100\n",
    "a.attack(b)\n",
    "print(b.blood)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 类的继承"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.002405Z",
     "start_time": "2021-11-28T01:43:20.996405Z"
    }
   },
   "outputs": [],
   "source": [
    "class Arthur(Warrior):\n",
    "    def __init__(self, name): # 实例化的传入的名字\n",
    "        self.name = name\n",
    "        self.blood = 100\n",
    "        self.damage = 31\n",
    "        self.defance = 30\n",
    "        self.x = 0\n",
    "        self.y = 0\n",
    "        self.speed = 60\n",
    "        \n",
    "    def shield(self):\n",
    "        self.defance += 10\n",
    "        print('{name}使用了 《圣盾》 技能，防御力增加 10 '.format(name = self.name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.019405Z",
     "start_time": "2021-11-28T01:43:21.006405Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aa = Arthur('aa')\n",
    "aa.speed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.033405Z",
     "start_time": "2021-11-28T01:43:21.021405Z"
    }
   },
   "outputs": [],
   "source": [
    "a = Arthur('Arthur')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.047405Z",
     "start_time": "2021-11-28T01:43:21.035405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "名字：Arthur\n",
      "血量：100\n",
      "攻击力：31\n",
      "防御力：30\n"
     ]
    }
   ],
   "source": [
    "a = Arthur('Arthur')\n",
    "print('名字：{}'.format(a.name))\n",
    "print('血量：{}'.format(a.blood))\n",
    "print('攻击力：{}'.format(a.damage))\n",
    "print('防御力：{}'.format(a.defance))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.060405Z",
     "start_time": "2021-11-28T01:43:21.050405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Arthur使用了 《圣盾》 技能，防御力增加 10 \n",
      "防御力：40\n"
     ]
    }
   ],
   "source": [
    "a.shield()\n",
    "print('防御力：{}'.format(a.defance))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 类的多态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.102405Z",
     "start_time": "2021-11-28T01:43:21.095405Z"
    }
   },
   "outputs": [],
   "source": [
    "class SuperWarrior(Warrior):\n",
    "    def attack(self,targert):\n",
    "    # 发起攻击\n",
    "        targert.blood -= self.damage + 10 -  targert.defance\n",
    "#         targert.blood = targert.blood -  (self.damage -  targert.defance)\n",
    "        print('{name} 攻击 {targert} ，造成 {hurt} 点伤害。'.format(\n",
    "            name = self.name,\n",
    "            targert = targert.name,\n",
    "            hurt = self.damage -  targert.defance + 10,\n",
    "            )\n",
    "         ) #这里的self.name就是获取我们传入的name  \n",
    "    \n",
    "    def medicine(self):\n",
    "        # 恢复技能\n",
    "        blood = self.blood\n",
    "        if self.blood  < 50:\n",
    "            self.blood += 40 + 10\n",
    "            recover_point = 40 + 10\n",
    "        else:\n",
    "            self.blood = 100\n",
    "            recover_point = 100 - blood\n",
    "        print('{name} 恢复了 {point} 点生命。当前生命值: {blood} '.format(\n",
    "            name = self.name, \n",
    "            point = recover_point, \n",
    "            blood = self.blood,\n",
    "            )\n",
    "         )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.117405Z",
     "start_time": "2021-11-28T01:43:21.105405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "攻击力：31\n",
      "攻击力：31\n"
     ]
    }
   ],
   "source": [
    "a = Warrior('Warrior')\n",
    "b = SuperWarrior('SuperWarrior')\n",
    "print('攻击力：{}'.format(a.damage))\n",
    "print('攻击力：{}'.format(b.damage))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.130405Z",
     "start_time": "2021-11-28T01:43:21.119405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Warrior 攻击 SuperWarrior ，造成 1 点伤害。\n",
      "SuperWarrior 攻击 Warrior ，造成 11 点伤害。\n"
     ]
    }
   ],
   "source": [
    "a.attack(b)\n",
    "b.attack(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.145405Z",
     "start_time": "2021-11-28T01:43:21.133405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Warrior 恢复了 40 点生命。当前生命值: 90\n",
      "SuperWarrior 恢复了 50 点生命。当前生命值: 100 \n"
     ]
    }
   ],
   "source": [
    "a.blood = 50\n",
    "b.blood = 50\n",
    "a.medicine()\n",
    "b.medicine()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课后练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "继承战士类Warrior自己改写一个刺客类Assassin,需要改写刺客的面板属性，攻击力强比战士多10点，防御力比战士少10点。\n",
    "刺客有两个技能：\n",
    " 《专注》 使用后可以提升10点攻击力\n",
    " 《萃毒》 使用后武器带有毒性，收到攻击者还会受到3次持续伤害，每次5点伤害\n",
    " 最后用代码进行测试，看看技能是否能够产生要求的效果。\n",
    " (Tip:为了实现武器带毒效果，你必须在类属性里增加一个武器是否带毒的属性,还要改写attack方法，判断武器是否带毒，以便产生不同的攻击效果。)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.269405Z",
     "start_time": "2021-11-28T01:43:21.258405Z"
    }
   },
   "outputs": [],
   "source": [
    "class Assassin (Warrior):\n",
    "    def __init__(self, name): # 实例化的传入的名字\n",
    "        self.name = name\n",
    "        self.blood = 100\n",
    "        self.damage = 41\n",
    "        self.defance = 20\n",
    "        self.x = 0\n",
    "        self.y = 0\n",
    "        self.poisonous = False\n",
    "        self.damageing = 0\n",
    "        \n",
    "    def attack(self,targert):\n",
    "        if self.damageing ==0:\n",
    "            self.damage=41\n",
    "        else:\n",
    "            self.damage =51\n",
    "            self.damageing -=1\n",
    "        if self.poisonous == False:\n",
    "            targert.blood -= self.damage -  targert.defance\n",
    "            #         targert.blood = targert.blood -  (self.damage -  targert.defance)\n",
    "            print('{name} 攻击 {targert} ，造成 {hurt} 点伤害。'.format(\n",
    "                name = self.name,\n",
    "                targert = targert.name,\n",
    "                hurt = self.damage -  targert.defance,\n",
    "                )\n",
    "             )\n",
    "        else:\n",
    "            targert.blood -= self.damage -  targert.defance\n",
    "            print('{name} 攻击 {targert} ，造成 {hurt} 点伤害。'.format(name = self.name,targert = targert.name,hurt = self.damage -  targert.defance))\n",
    "            for i in range(3):\n",
    "                targert.blood -=5\n",
    "                print('{name}的攻击使{targert}中毒造成{blood}伤害'.format(name=self.name,targert=targert.name,blood=5))\n",
    "            \n",
    "                \n",
    "    def poison (self):\n",
    "        self.poisonous=True\n",
    "        print('{name}使用了《淬毒》技能'.format(name=self.name))\n",
    "        \n",
    "    def attention (self):\n",
    "        self.damageing=3\n",
    "        print('{name}使用了《专注》技能，攻击力提高了10点'.format(name=self.name))      \n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.337405Z",
     "start_time": "2021-11-28T01:43:21.271405Z"
    }
   },
   "outputs": [],
   "source": [
    "a=Warrior('a')\n",
    "b=Assassin('b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.355405Z",
     "start_time": "2021-11-28T01:43:21.340405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b 攻击 a ，造成 11 点伤害。\n",
      "b 攻击 a ，造成 11 点伤害。\n"
     ]
    }
   ],
   "source": [
    "b.attack(a)\n",
    "b.attack(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:21.370405Z",
     "start_time": "2021-11-28T01:43:21.357405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b使用了《专注》技能，攻击力提高了10点\n",
      "b 攻击 a ，造成 21 点伤害。\n",
      "b 攻击 a ，造成 21 点伤害。\n",
      "b 攻击 a ，造成 21 点伤害。\n",
      "b 攻击 a ，造成 11 点伤害。\n"
     ]
    }
   ],
   "source": [
    "b.attention()\n",
    "b.attack(a)\n",
    "b.attack(a)\n",
    "b.attack(a)\n",
    "b.attack(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-28T01:43:37.496405Z",
     "start_time": "2021-11-28T01:43:37.485405Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b使用了《专注》技能，攻击力提高了10点\n",
      "b使用了《专注》技能，攻击力提高了10点\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "41"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.attention()\n",
    "b.attention()\n",
    "b.damage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-17T08:55:48.933200Z",
     "start_time": "2021-11-17T08:55:48.929200Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b使用了《淬毒》技能\n",
      "b 攻击 a ，造成 11 点伤害。\n",
      "b的攻击使a中毒造成5伤害\n",
      "b的攻击使a中毒造成5伤害\n",
      "b的攻击使a中毒造成5伤害\n"
     ]
    }
   ],
   "source": [
    "b.poison()\n",
    "b.attack(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "341.458px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
